<?php

class Tourism_goods_base_model extends CI_Model
{
    private $table = 'tourism_goods_base';
    private $table_2 = 'tourism_goods_category';
    private $table_3 = 'tourism_goods_attr';
    private $table_5 = 'region';

    public function findByGoodsAttrId($goodsAttrId)
    {
        $this->db->select('tourism_goods_base.goods_name,tourism_goods_base.detail,tourism_goods_base.uid,tourism_goods_base.transport_id,tourism_goods_base.transport_cost,tourism_goods_base.goods_spec,tourism_goods_base.wap_goods_desc,tourism_goods_base.limit_num,tourism_goods_base.limit_day,tourism_goods_base.is_on_sale,tourism_goods_attr.is_check,tourism_goods_attr.swim,tourism_goods_attr.goods_num,tourism_goods_attr.shop_price,tourism_goods_attr.supply_price,tourism_goods_attr.market_price,tourism_goods_attr.sale_count,tourism_goods_attr.goods_attr_id,tourism_goods_attr.attribute_key,tourism_goods_attr.weight,tourism_goods_base.images');
        $this->db->from($this->table . ' as tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');
        $this->db->where('tourism_goods_attr.goods_attr_id', $goodsAttrId);
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        $this->db->limit(1);

        return $this->db->get();
    }

    /**
     * @descripte   移动端预览页面
     * @Author xiumao
     * @date 2016/6/16 0016 上午 11:42
     */
    public function findByGoodsAttrIdPre($goodsAttrId)
    {
        $this->db->select('tourism_goods_base.goods_name,tourism_goods_base.detail,tourism_goods_base.uid,tourism_goods_base.transport_id,tourism_goods_base.transport_cost,tourism_goods_base.goods_spec,tourism_goods_base.wap_goods_desc,tourism_goods_base.limit_num,tourism_goods_base.is_on_sale,tourism_goods_attr.is_check,tourism_goods_attr.swim,tourism_goods_attr.goods_num,tourism_goods_attr.shop_price,tourism_goods_attr.supply_price,tourism_goods_attr.market_price,tourism_goods_attr.sale_count,tourism_goods_attr.goods_attr_id,tourism_goods_attr.attribute_key,tourism_goods_attr.weight,tourism_goods_base.images');
        $this->db->from($this->table . ' as tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');
        $this->db->where('tourism_goods_attr.goods_attr_id', $goodsAttrId);
        $this->db->limit(1);

        return $this->db->get();
    }

    /**
     * 判断是否存在
     * @param $goodsAttrId
     * @return mixed
     */
    public function isExistProduct($goodsAttrId)
    {
        $this->db->where('tourism_goods_attr.goods_attr_id', $goodsAttrId);
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->join('tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');

        return $this->db->count_all_results($this->table_3);
    }

    /**
     * 搜索列表
     * @param $count
     * @param int $num
     * @param array $param
     * @param array $ids
     * @return mixed
     */
    public function search($count, $num = 0, $param = [], $ids = [])
    {
        $cid = isset($param['cid']) ? $param['cid'] : 0;
        $param['sort'] = isset($param['sort']) ? $param['sort'] : 1;
        $this->db->select('`region`.`region_name`,`tourism_goods_attr`.`goods_attr_id`, `tourism_goods_attr`.`goods_id`,
                          `tourism_goods_attr`.`market_price`, `tourism_goods_attr`.`shop_price`, `tourism_goods_attr`.`swim` ,`tourism_goods_attr`.`sale_count`,
                           `tourism_goods_base`.`images`, `tourism_goods_base`.`goods_name`,`tourism_goods_base`.`transport_id`,`tourism_goods_base`.`transport_cost`');
        $this->db->from('tourism_goods_base');
        $this->db->join('tourism_goods_attr', 'tourism_goods_attr.goods_id=tourism_goods_base.goods_id', 'inner');
        $this->db->join('region', 'tourism_goods_base.province_id=region.region_id', 'inner');
        if (!empty($param['keyword'])) {
            $keyword = trim(addslashes($param['keyword']));
            $this->db->where('((tourism_goods_base.goods_name LIKE "%' . $keyword . '%") or (tourism_goods_base.detail LIKE "%' . $keyword . '%") or (tourism_goods_base.keyword LIKE "%' . $keyword . '%"))');
        }
        if (isset($cid) && (int)$cid != 0) {
            if (count($ids) > 0) {
                $this->db->where_in('tourism_goods_base.category_id', $ids);
            } else {
                $this->db->where('tourism_goods_base.category_id', $cid);
            }
        }
        $this->db->order_by('tourism_goods_attr.sort', 'desc');
        if (isset($param['sort'])) {
            switch ($param['sort']) {
                case 1:
                    $this->db->order_by('tourism_goods_attr.shop_price', 'asc');
                    $this->db->order_by('tourism_goods_attr.sale_count', 'desc');
                    break;
                case 2:
                    $this->db->order_by('tourism_goods_attr.sale_count', 'desc');
                    break;
                case 3:
                    $this->db->order_by('tourism_goods_attr.sale_count', 'asc');
                    break;
                case 4:
                    $this->db->order_by('tourism_goods_attr.shop_price', 'desc');
                    break;
                case 5:
                    $this->db->order_by('tourism_goods_attr.shop_price', 'asc');
                    break;
                default:
                    $this->db->order_by('tourism_goods_attr.swim', 'desc');
                    break;
            }
        }
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_attr.logic_delete', 1);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        $this->db->limit($count, $num);
        return $this->db->get();
    }

    /**
     * 首页销量排行榜
     */
    public function findBySalesRank($limit = 12)
    {
        $this->db->select('tourism_goods_attr.sale_count,tourism_goods_base.detail,tourism_goods_base.goods_name,tourism_goods_attr.goods_attr_id,tourism_goods_attr.shop_price,tourism_goods_base.images');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where('tourism_goods_base.show_flag', 2); //商品首页产品推荐
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核
        $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
        $this->db->limit($limit);

        return $this->db->get();
    }

    /**
     * 首页特产推荐  按添加时间
     */
    public function findLocalSpecial($limit = 6, $params = [])
    {
        $this->db->select('tourism_goods_base.detail,tourism_goods_base.goods_name,tourism_goods_attr.goods_attr_id,tourism_goods_attr.shop_price,tourism_goods_base.images');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where_not_in('tourism_goods_base.show_flag', array(1, 2)); //商品首页产品推荐
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核
        if ($params) {
            $this->db->where($params);
        }
        $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
        $this->db->limit($limit);

        return $this->db->get();
    }

    /*
     * 商家推荐
     * */
    public function findShopRecommend($num, $goodsAttrId, $cid)
    {
        $this->db->select('tourism_goods_base.goods_name,tourism_goods_attr.sale_count,tourism_goods_attr.goods_attr_id,tourism_goods_attr.shop_price,tourism_goods_base.images');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' AS tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        if (!empty($goodsAttrId)) {
            $this->db->where('tourism_goods_attr.goods_attr_id <>', $goodsAttrId);
        }
        if (!empty($cid)) {
            $this->db->where('tourism_goods_base.category_id', $cid);
        }
        $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
        $this->db->limit($num);

        return $this->db->get();
    }

    /**
     * 推荐一个景区的供应商的相关产品
     * @param unknown $pg_num
     * @param unknown $num
     * @param number $uid
     */
    public function findRecommend($pg_num, $num, $uid = 0)
    {
        $this->db->select('b.images,a.goods_attr_id,a.shop_price,b.goods_name');
        $this->db->from($this->table . ' as b');
        $this->db->join($this->table_3 . ' as a', 'a.goods_id = b.goods_id');
        $this->db->where('a.is_check', 2);
        $this->db->where('b.is_on_sale', 1);
        if (!empty($uid)) {
            $this->db->where('b.uid', $uid);
        }
        $this->db->order_by('a.sale_count', 'DESC');
        $this->db->limit($pg_num, $num);

        return $this->db->get();
    }

    /*
     * 商品收藏数
     * */
    public function enshrine($aid)
    {
        $this->db->where('goods_attr_id', $aid);
        return $this->db->count_all_results($this->table_3);
    }

    /**
     * 查找省份的本地特产产品
     */
    public function findByRegionProduct($province_id, $count, $num)
    {
        $this->db->select('tourism_goods_base.province_id AS "region_id", tourism_goods_base.detail,tourism_goods_base.goods_name,tourism_goods_attr.goods_attr_id,tourism_goods_attr.shop_price,tourism_goods_base.images,tourism_goods_attr.sale_count');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where('tourism_goods_base.province_id', $province_id); //所在省份
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核
        $this->db->order_by('tourism_goods_attr.created_at', 'DESC');
        $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
        $this->db->limit($count, $num);

        return $this->db->get();
    }

    /**
     * 查找省份的本地特产产品
     */
    public function regionProductTotal($province_id)
    {
        $this->db->select('*');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where('tourism_goods_base.province_id', $province_id); //所在省份
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核

        return $this->db->count_all_results();
    }

    /**
     * 商铺推荐产品-和-每日特价
     * @param array $param
     * @param $pageNum
     * @param $num
     * @return mixed
     */
    public function findProductByUidAndCategory($param = array(), $pageNum, $num)
    {
        $this->db->select('region.region_name,tourism_goods_base.category_id,tourism_goods_base.detail,tourism_goods_base.province_id,tourism_goods_attr.goods_attr_id,tourism_goods_attr.goods_id,tourism_goods_attr.sale_count,tourism_goods_attr.market_price,tourism_goods_attr.shop_price,tourism_goods_attr.goods_num,tourism_goods_attr.created_at,tourism_goods_base.images,tourism_goods_base.goods_name,tourism_goods_base.transport_id,tourism_goods_base.transport_cost');
        $this->db->from('tourism_goods_attr');
        $this->db->join('tourism_goods_base', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        if ((isset($param['ismain']) && !empty($param['ismain'])) || (isset($param['category_id']) && !empty($param['category_id']))) {
            $this->db->join('tourism_provider_category', 'tourism_goods_base.provider_category_id=tourism_provider_category.id');
        }
        if (!empty($param['keyword'])) {
            $keyword = trim(addslashes($param['keyword']));
            $this->db->where('((tourism_goods_base.goods_name LIKE "%' . $keyword . '%") or (tourism_goods_base.detail LIKE "%' . $keyword . '%") or (tourism_goods_base.keyword LIKE "%' . $keyword . '%"))');
        }
        $this->db->join('region', 'tourism_goods_base.province_id = region.region_id');
        if (isset($param['uid']) && !empty($param['uid'])) {
            $this->db->where('tourism_goods_base.uid', $param['uid']);
        }
        if (isset($param['ismain']) && !empty($param['ismain'])) {
            $this->db->where('tourism_provider_category.is_main', 2);
        }
        if (isset($param['category_id']) && !empty($param['category_id'])) {
            $this->db->where('tourism_goods_base.provider_category_id', $param['category_id']);
        }
        if ((isset($param['ismain']) && !empty($param['ismain'])) || (isset($param['category_id']) && !empty($param['category_id']))) {
            $this->db->where('tourism_provider_category.is_on_sale', 1);
        }
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        if (isset($param['sort']) && !empty($param['sort'])) {
            switch ($param['sort']) {

                case '3':
                    $this->db->order_by('tourism_goods_attr.sale_count', 'ASC');
                    break;
                case '4':
                    $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
                    break;
                case '6'://综合排序
                    $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
                    $this->db->order_by('tourism_goods_attr.tour_count', 'DESC');
                    break;
                case '7':
                    $this->db->order_by('tourism_goods_attr.shop_price', 'ASC');
                    break;
                case '8':
                    $this->db->order_by('tourism_goods_attr.shop_price', 'DESC');
                    break;
                default;
                    $this->db->order_by('tourism_goods_attr.tour_count', 'DESC');//浏览数
                    break;
            }
        }
        $this->db->limit($pageNum, $num);

        return $this->db->get();
    }

    /**
     * 查询金币商品
     */
    public function findBzCurrencyTourism($params)
    {
        $this->db->select('`tourism_goods_attr`.`goods_attr_id`, `tourism_goods_attr`.`goods_id`,`tourism_goods_attr`.`market_price`, `tourism_goods_attr`.`shop_price`, `tourism_goods_base`.`images`, `tourism_goods_base`.`goods_name`, swim, sale_count');
        $this->db->from('tourism_goods_base');
        $this->db->join('tourism_goods_attr', 'tourism_goods_attr.goods_id=tourism_goods_base.goods_id', 'inner');
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        if (!empty($params['swim'])) {
            $this->db->where('swim > 0');
        }
        if (!empty($params['goods_native'])) {
            $this->db->where('tourism_goods_base.goods_native', $params['goods_native']); //是否是特产
        }
        if (isset($params['full_bonus'])) {
            if ($params['full_bonus'] == 1) {
                $this->db->where('tourism_goods_attr.swim = tourism_goods_attr.shop_price');
            } elseif ($params['full_bonus'] == 2) {
                $this->db->where('tourism_goods_attr.swim <> tourism_goods_attr.shop_price');
            }
        }
        if (!empty($params['province_id'])) {
            $this->db->where('tourism_goods_base.province_id', $params['province_id']); //所在省份
        }
        if (!empty($params['goods_sn'])) {
            $this->db->where_in('tourism_goods_base.goods_sn', $params['goods_sn']); //商品编号
        }
        if (!empty($params['city_id'])) {
            $this->db->where('tourism_goods_base.city_id', $params['city_id']); //所在城市
        }
        if (isset($params['sort'])) {
            switch ($params['sort']) {
                case 1:
                    $this->db->order_by('tourism_goods_attr.swim', 'DESC');
                    break;
                case 2:
                    $this->db->order_by('tourism_goods_attr.swim', 'ASC');
                    break;
                case 3:
                    $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
                    break;
                default:
                    break;
            }
        }
        if (!empty($params['limit'])) {
            $this->db->limit($params['limit']);
        }

        return $this->db->get();
    }

    /**
     * @descripte   大首页特产推荐
     * @Author xiumao
     * @date 2016/7/18 上午 10:34
     */
    public function findIndexSpecial($limit)
    {
        $this->db->select('tourism_goods_attr.sale_count,tourism_goods_base.goods_name,tourism_goods_attr.goods_attr_id,tourism_goods_attr.shop_price,tourism_goods_base.images');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where_not_in('tourism_goods_base.show_flag', 1); //商品大首页产品推荐
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核
        $this->db->order_by('tourism_goods_attr.sale_count', 'DESC');
        $this->db->limit($limit);

        return $this->db->get();
    }

    /**
     * 2016 11.1 新首页 搜索列表
     * @param unknown $count
     * @param number $num
     * @param unknown $param
     */
    public function findListByRegionId($page_num, $num, $regionId)
    {
        $this->db->select('`region`.`region_name`,`tourism_goods_attr`.`goods_attr_id`, `tourism_goods_attr`.`goods_id`,
                          `tourism_goods_attr`.`market_price`, `tourism_goods_attr`.`shop_price`, `tourism_goods_attr`.`swim` ,`tourism_goods_attr`.`sale_count`,
                           `tourism_goods_base`.`images`, `tourism_goods_base`.`goods_name`,`tourism_goods_base`.`transport_id`,`tourism_goods_base`.`transport_cost`');
        $this->db->from('tourism_goods_base');
        $this->db->join('tourism_goods_attr', 'tourism_goods_attr.goods_id=tourism_goods_base.goods_id', 'inner');
        $this->db->join('region', 'tourism_goods_base.city_id=region.region_id', 'inner');
        $this->db->where('region.region_id', $regionId);
        $this->db->where('tourism_goods_attr.is_check', 2);
        $this->db->where('tourism_goods_attr.logic_delete', 1);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }


    /**
     * 查找省 市 区的本地特产产品
     */
    public function findRegionProduct($params, $page_num, $num)
    {
        $this->db->select('`tourism_goods_attr`.`goods_attr_id`, `tourism_goods_attr`.`goods_id`,`tourism_goods_base`.`province_id`, `tourism_goods_base`.`city_id`, 
                          `tourism_goods_attr`.`market_price`, `tourism_goods_attr`.`shop_price`, `tourism_goods_attr`.`swim` ,`tourism_goods_attr`.`sale_count`,
                           `tourism_goods_base`.`images`, `tourism_goods_base`.`goods_name`,`tourism_goods_base`.`transport_id`,`tourism_goods_base`.`transport_cost`');
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_3 . ' as tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        if (!empty($params['province_id'])) {
            $this->db->where('tourism_goods_base.province_id', $params['province_id']); //所在省份
        }
        if (!empty($params['city_id'])) {
            $this->db->where('tourism_goods_base.city_id', $params['city_id']); //所在城市
        }
        if (!empty($params['district_id'])) {
            $this->db->where('tourism_goods_base.district_id', $params['district_id']); //所在县
        }
        $this->db->where('tourism_goods_attr.is_check', 2); //通过审核
        $this->db->where('tourism_goods_attr.logic_delete', 1);
        $this->db->where('tourism_goods_base.is_on_sale', 1); //已上线
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }

}